<?php
/**
 * Add feed procedure
 *
 * @author University of London Computer Centre
 * @version 1.1.0
 * @package ArchivePress
 * @subpackage pages
 * @global ArchivePress $ArchivePress
 */

// globals
global $ArchivePress;

// actions URL
$responder = get_bloginfo( 'wpurl' ).'/wp-admin/admin.php?page=archivepress/pages/responder.php';

// CHECK FOR MESSAGES
$message = "The Whole Procedure is coming soon, this is just to let you add feeds using the Basic Procedure";

// added
if(array_key_exists("added", $_GET)){
    $message = __("Feed Added", ARCHIVEPRESS);
}


if($message != ""){
    $message = '<div class="updated fade below-h2" id="message">
                    <p>
                        '.$message.'<br />
                    </p>
                </div>';
}

echo '<div class="wrap">
        <div class="icon32" id="icon-archivepress"><br /></div>
        <h2>'.__("Add Feed", ARCHIVEPRESS).'</h2>
        '.$message.'
        <form method="post" action="admin.php?page=archivepress/pages/add-feed.php">
            <input type="hidden" name="back" value="add-feed" />
            <input type="hidden" name="' . WP_ARCHIVEPRESS_PREFIX . 'action" value="addfeed" />';

// security
wp_nonce_field(WP_ARCHIVEPRESS_PREFIX . 'addfeed');

// Feed Settings
echo'
            <table class="form-table">
                <tr valign="top">
                    <th scope="row"><label for="' . WP_ARCHIVEPRESS_PREFIX . 'feedname">'.__("Feed Name").'</label></th>
                    <td>
                        <input type="text" class="regular-text" id="' . WP_ARCHIVEPRESS_PREFIX . 'feedname" name="' . WP_ARCHIVEPRESS_PREFIX . 'feedname" value="" />
                    </td>
                </tr>

                <tr valign="top">
                    <th scope="row"><label for="' . WP_ARCHIVEPRESS_PREFIX . 'feedurl">'.__("Feed Url").'</label></th>
                    <td>
                        <input type="text" class="regular-text" id="' . WP_ARCHIVEPRESS_PREFIX . 'feedurl" name="' . WP_ARCHIVEPRESS_PREFIX . 'feedurl" value="http://" />
                        <span class="description">'.__("This can be an Atom, RSS or blog url.", ARCHIVEPRESS).'</span>
                    </td>
                </tr>

            </table>

            <p class="submit">
            <input type="submit" class="button-primary" value="'.__('Add Feed', ARCHIVEPRESS).'" />
            </p>';

echo '
        </form>
     </div>';

// @todo
if(1 == 2) {
    $testURL = $_POST["url"];
    $testNAME = $_POST["name"];

    $feedAdded = false;
    $lastSentence = "";
    // ADD NEW FEED PROCEDURE
    // check if is a feed url
    try {
        if($ArchivePress->isFeed($testURL)){
            try {
                $ArchivePress->addLastCheckedFeed($testNAME,true);
                $feedAdded = true;
                $lastSentence = "DISPLAY SUCCESS: feed successfully added";
            } catch (Exception $e) {
                $lastSentence = $e->getMessage();
                $feedAdded = false;
            }
        } else {
            // try autodiscovery
            $feeds = array();
            try {
                $feeds = FeedAutoDiscovery::getFeedLinks($testURL);
                // If the feed was not already added
                if($ArchivePress->isFeedNew($feeds)) {
                    // Add Feed
                    try {
                        $ArchivePress->addFeed($feeds,$testURL,$testNAME);
                        $feedAdded = true;
                        $lastSentence = "DISPLAY SUCCESS: feed successfully added";
                    } catch (Exception $e) {
                        $lastSentence = $e->getMessage();
                        $feedAdded = false;
                    }
                } else {
                    $lastSentence = "DISPLAY ERROR: feed already added!";
                }
            } catch (Exception $e) {
                $lastSentence = $e->getMessage();
                $feedAdded = false;
            }
        }
    } catch (Exception $e) {
        // page not found or other curl errors
        $lastSentence = $e->getMessage();
        $feedAdded = false;
    }
    echo $lastSentence."<br><br><br>";


}

?>